package com.ruoyi.user.controller;

import java.io.InputStream;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.config.AliyunOSSUtils;
import com.ruoyi.user.domain.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.UserAnchor;
import com.ruoyi.user.service.IUserAnchorService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 主播管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/anchor")
public class UserAnchorController extends BaseController
{
    @Autowired
    private IUserAnchorService userAnchorService;

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;// 阿里云OSS

    /**
     * 查询主播管理列表
     */
    @RequiresPermissions("user:anchor:list")
    @GetMapping("/list")
    public TableDataInfo list(UserAnchor userAnchor)
    {
        startPage();
        List<UserAnchor> list = userAnchorService.selectUserAnchorList(userAnchor);
        return getDataTable(list);
    }

    /**
     * 导出主播管理列表
     */
    @RequiresPermissions("user:anchor:export")
    @Log(title = "主播管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAnchor userAnchor)
    {
        List<UserAnchor> list = userAnchorService.selectUserAnchorList(userAnchor);
        ExcelUtil<UserAnchor> util = new ExcelUtil<UserAnchor>(UserAnchor.class);
        util.exportExcel(response, list, "主播管理数据");
    }

    /**
     * 获取主播管理详细信息
     */
    @RequiresPermissions("user:anchor:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userAnchorService.selectUserAnchorById(id));
    }

    /**
     * 新增主播管理
     */
    @RequiresPermissions("user:anchor:add")
    @Log(title = "主播管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAnchor userAnchor)
    {
        return toAjax(userAnchorService.insertUserAnchor(userAnchor));
    }

    /**
     * 修改主播管理
     */
    @RequiresPermissions("user:anchor:edit")
    @Log(title = "主播管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAnchor userAnchor)
    {
        return toAjax(userAnchorService.updateUserAnchor(userAnchor));
    }

    /**
     * 删除主播管理
     */
    @RequiresPermissions("user:anchor:remove")
    @Log(title = "主播管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAnchorService.deleteUserAnchorByIds(ids));
    }

    //图片上传  头像
    //图片上传下载
    @RequestMapping("fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }

    //认证
    @RequestMapping("zh/{id}")
    public AjaxResult zh(@PathVariable("id") Integer id) {
        userAnchorService.updateUserRz(id);
        return success();
    }

    //查询直播内容
    @RequestMapping("zb/{id}")
    public AjaxResult zb(@PathVariable("id") Integer id) {
        List<UserVO> userAnchorList = userAnchorService.zb(id);
        return success(userAnchorList);
    }
}
